// @flow
import React from 'react'
import styles from './ContactList.css'
import { Table, Select, message, Input, Button } from 'antd'
const Option = Select.Option
import R from 'ramda'
import EditContact from './EditContact'
import AddContact from './AddContact'
import ReactDom from 'react-dom'
// import universalFetch, { handleFetchError } from 'store/modules/fetch'
message.config({
  top: 100
})

type Props = {
  contactData: Array<Object>,
  customerId: string,
  getContact: Function
}
type States = {
  contactData: Array<Object>
}
class ContactList extends React.Component {
  state: States
  props: Props
  addContact: Function
  deleteContact: Function
  selectParts: Function
  editContact: Function
  changeContact: Function
  removeEditPoint: Function
  constructor (props: Props) {
    super(props)
    this.state = {
      contactData: this.props.contactData || []
    }
    this.addContact = this.addContact.bind(this)
    this.deleteContact = this.deleteContact.bind(this)
    this.editContact = this.editContact.bind(this)
    this.removeEditPoint = this.removeEditPoint.bind(this)
    this.changeContact = this.changeContact.bind(this)
  }
  componentWillMount () {
    const { contactData } = this.props
    if (contactData) {
      this.setState({
        contactData: contactData
      })
    }
  }
  componentWillReceiveProps (nextProps: Object) {
    if (!R.equals(nextProps, this.props)) {
      const { contactData } = nextProps
      if (contactData) {
        this.setState({
          contactData: contactData
        })
      }
    }
  }
  removeEditPoint () {
    const container = this.refs.editContact
    ReactDom.unmountComponentAtNode(container)
  }
  changeContact (data: Array<Object>) {
    this.setState({
      contactData: data
    })
    this.props.getContact(data)
  }
  deleteContact (item: Object) {
    const { contactData } = this.state
    let arr = R.clone(contactData)
    const l = contactData.indexOf(item)
    if (l > -1) {
      arr.splice(l, 1) // 删除数组中的这一项
      this.setState({
        contactData: arr
      })
      this.props.getContact(arr)
    }
  }
  editContact (item: Object) {
    const { contactData } = this.state
    const index = contactData.indexOf(item) // 知道其为第几项
    const container = this.refs.editContact
    ReactDom.render(
      <EditContact {...{
        index,
        data: item,
        contactData,
        changeContact: this.changeContact,
        removeNode: this.removeEditPoint
      }} />,
    container
    )
  }
  addContact () {
    const { customerId } = this.props
    const { contactData } = this.state
    const index = contactData.length
    const container = this.refs.editContact
    ReactDom.render(
      <AddContact {...{
        index,
        customerId,
        contactData,
        changeContact: this.changeContact,
        removeNode: this.removeEditPoint
      }} />,
    container
    )
  }
  render () {
    const { contactData } = this.state
    const component = this
    const columns = [{
      title: '姓名',
      dataIndex: 'name',
      key: 'name',
      width: '26%'
    }, {
      title: '手机号',
      dataIndex: 'phoneNum',
      key: 'phoneNum',
      width: '26%'
    }, {
      title: '默认联系人',
      dataIndex: 'importance',
      key: 'importance',
      width: '26%'
    }, {
      title: '操作',
      dataIndex: 'operate',
      key: 'operate',
      width: '20%'
    }]
    const contactText = contactData.map((item, index) => {
      return {
        key: index,
        name: item.name,
        phoneNum: item.phoneNum,
        importance: item.importance === 'primary' ? '默认联系人' : '',
        operate: <label>
          <span
            className={styles['operate']}
            style={{ marginRight: 10 }}
            onClick={function () { component.editContact(item) }}
            title='编辑'>
            <i className='fa fa-pencil-square-o' aria-hidden='true' />
          </span>
          <span className={styles['operate']} title='删除联系人'
            onClick={function () { component.deleteContact(item) }}>
            <i className='fa fa-minus-circle' aria-hidden='true' />
          </span>
        </label>
      }
    })
    const dataSource = [{
      key: 10000,
      name: <div style={{ position:'relative', height:30 }}>
        <Button style={{ position:'absolute', left:0, top:0 }} onClick={this.addContact}>添加联系人</Button>
      </div>
    }]
    return (
      <div className={styles['main']}>
        <Table showHeader={false} columns={columns} pagination={false} dataSource={dataSource.concat(contactText)} />
        <div ref='editContact' />
      </div>
    )
  }
}

export default ContactList
